package com.cn.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.mapper.CategoryMapper;
import com.cn.pojo.TzCategory;
import com.cn.service.CategoryService;
import com.cn.utils.RequestPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import red.zyc.desensitization.Sensitive;
import red.zyc.desensitization.annotation.ChineseNameSensitive;
import red.zyc.desensitization.annotation.EmailSensitive;
import red.zyc.desensitization.support.TypeToken;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, TzCategory> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     *  System.out.printf("集合脱敏: %s%n",
     *  Sensitive.desensitize(Stream.of("123456@qq.com", "1234567@qq.com", "1234568@qq.com").collect(Collectors.toList()),
     *                 new TypeToken<List<@EmailSensitive String>>() {
     *                 }));
     * ————————————————
     * 版权声明：本文为CSDN博主「初夏0811」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
     * 原文链接：https://blog.csdn.net/m0_37899908/article/details/131271740
     */
    @Override
    public List<TzCategory> categoryPage(RequestPage requestPage) {
        Page<TzCategory> page = new Page<>();
        page.setPages(requestPage.getPage());
        page.setSize(requestPage.getSize());
        List<TzCategory> tzCategories = categoryMapper.selectListKey(requestPage.getKey());
        return tzCategories;
    }

    @Override
    public List<TzCategory> categoryById(Integer id) {
        return categoryMapper.selectList(new LambdaQueryWrapper<TzCategory>().eq(TzCategory::getParentId,id));
    }

    @Override
    public List<TzCategory> cateParent() {
        return categoryMapper.selectList(new LambdaQueryWrapper<TzCategory>().eq(TzCategory::getParentId,0));
    }

    /**
     * 添加分类
     * @param tzCategory
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addCategory(TzCategory tzCategory) {
        tzCategory.setRecTime(new Date());
        tzCategory.setUpdateTime(new Date());
        return  categoryMapper.insert(tzCategory);
    }
}
